

const store = {
    // 定义状态：
    state:{
        count:5
    },

    // 管理状态
    reducer:function(action){
        switch(action.type){
            case "ADD":
                let obj = {
                    ...store.state,
                    count:store.state.count+action.payload
                };
                console.log("obj",obj);
                return obj;
            case "SUB":return {
                ...store.state,
                count:store.state.count-1
            }
            default:return store.state
        }
    },

    getState(){
        return store.state
    },

    dispatch(action){
        store.state  = store.reducer(action);
        store.publish();
    },

    // 所有的订阅
    listeners:[],

    subscribe(cb){
        store.listeners.push(cb);
    },

    publish(){
        store.listeners.forEach(cb=>{
            cb();
        })
    }

}

export default store;
